package com.example.wacoffee_user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.wacoffee_user.annotation.AutoFill;
import com.example.wacoffee_user.common.OperationType;
import com.example.wacoffee_user.pojo.entity.Good;
import com.example.wacoffee_user.pojo.entity.Order;
import com.example.wacoffee_user.pojo.entity.coupon;
import com.example.wacoffee_user.pojo.entity.item;
import com.example.wacoffee_user.pojo.vo.pointVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
   List<item> checkStock(item[] items);


   void batchDeductStock(item[] items);

   @AutoFill(OperationType.INSERT)
   void saveOrder(Order order);

   void addOrderToGoods(item[] items, Integer orderId);

   @Select("select id,information,expend,create_time,status from orders where user_id = #{currentId} order by create_time desc")
   List<Order> getAllOrder(Integer currentId);

   @Select("select id,information,expend,create_time,status from orders where user_id = #{currentId} and status = 0 order by create_time desc")
   List<Order> getNoPaidOrder(Integer currentId);

   @Select("select id,information,expend,create_time,status from orders where user_id = #{currentId} and status = 3 order by create_time desc")
   List<Order> getProcessedOrder(Integer currentId);

   @Select("select id,information,expend,create_time,status from orders where user_id = #{currentId} and status = 2 order by create_time desc")
   List<Order> getCanceledOrder(Integer currentId);

   @Update("update orders set status = 1 where id = #{id}")
   void payOrder(int id);

   int updateUserBalance(double expend, int userId);

   @Select("select g.id as id,og.consume as stock from order_good og left join good g on og.good_id = g.id where og.order_id=#{id}" )
   List<Good> getGoodsByOrderId(int id);

   void updateGoodStock(List<Good> goods);

   @Update("update coupon_order set status = 2 where id=#{uid}")
   void updateCouponStatus(int uid);

   @Select("select min_amount,`value`,status from coupon where id = #{couponId}")
   coupon getCouponById(Integer couponId);


   @Update("update user set point = point + #{point},exp =exp +#{point} where id = #{userId}")
   void addPoint(pointVo pointVo);

   @Update("update user set exp = exp - 1000,level = level + 1 where id = #{userId} and exp>=1000")
   int checkExp(Integer userId);

   @Insert("insert into coupon_order(user_id,coupon_id) values(#{userId},#{couponId})")
   void addLevelCoupon(Integer userId,int couponId);
}
